const express = require('express');
const path = require('path');
const { createAuthMiddleware } = require('./utils/auth'); // 导入认证模块

const app = express();
const port = 3030;

// 允许跨域
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  next();
});

// 创建认证中间件实例
const authMiddleware = createAuthMiddleware();

// 保护路由
app.use('/vtepobjlist', authMiddleware);
app.use('/vtfollowupep/index.html', authMiddleware);

// 导入路由
const indexRouter = require('./routes/index');
const obsSkyRouter = require('./routes/obsSkyRoutes');
const svomRouter = require('./routes/svomRoutes');
const vtRouter = require('./routes/vtRoutes');
const createVtTargetRouter = require('./routes/vtTargetRoutes');
const { TARGET_DIR } = require('./constants/paths');
const { TARGET_DIR_FOR_EP } = require('./constants/paths');

// 设置静态文件目录
app.use(express.static(path.join(__dirname, 'public')));

// 使用路由
app.use('/', indexRouter);
app.use('/obsSky', obsSkyRouter);
app.use('/api', svomRouter);
app.use('/api/vt', vtRouter);
app.use('/vttargets', createVtTargetRouter(TARGET_DIR));
app.use('/vtepobjlist', createVtTargetRouter(TARGET_DIR_FOR_EP));

// 启动服务器
app.listen(port, () => {
  console.log(`App listening at port ${port}: http://localhost:${port}`);
});